<!DOCTYPE html>
<html lang="zh_CN">
<head>
<#--引入外部文件-->
    <#include "../common/header.ftl">

    <script>
        // 删除选点保险类型及其名下产品
        $(function () {

            $(".delete_Btn").click(function () {

                //获取删除按钮中绑定的自定义JSON对象
                var obj = $(this).data("json");

                // 如果obj对象存在，删除该对象
                if (obj) {
                    $.messager.confirm("温馨提示", "确定删除该类及其名下产品吗?", function () {
                        // 删除成功刷新页面
                        $.get('/insurance/delete.do', {id: obj.id}, function (data) {
                            if (data.success) {
                                window.location.reload();
                            }
                        });

                    });
                }
            });
        });

    </script>

</head>
<body>

<div class="container" style="margin-top: 20px">
    <#include "../common/top.ftl">
    <div class="row">
        <div class="col-sm-3">
            <#include "../common/menu.ftl">
        </div>
        <div class="col-sm-9">
            <div class="row">
                <div class="col-sm-12">
                    <h1 class="page-head-line">保险类型管理</h1>
                </div>
            </div>
        <#--高级查询--->
            <form class="form-inline" id="searchForm" action="/insurance/list.do" method="post">
                <input type="hidden" name="currentPage" id="currentPage" value="1">
                <div class="form-group">
                    <label>关键字:</label>
                    <input type="text" class="form-control" name="keyword" value="${(qo.keyword)!}"
                           placeholder="请输入保险名称">
                </div>
                <button id="btn_query" class="btn btn-default">
                    <span class="glyphicon glyphicon-search"></span> 查询
                </button>
                <a href="javascript:" class="btn btn-success inputBtn">
                    <span class="glyphicon glyphicon-plus"></span> 添加保险类型
                </a>
            </form>

            <table class="table table-striped table-hover">
                <tr>
                    <th>编号</th>
                    <th>类型</th>
                    <th>订单量</th>
                    <th>销售额/￥</th>
                <#--<th>状态</th>-->
                    <th>操作</th>
                </tr>
            <#--使用Freemark的指令完成循环迭代-->
                <#list pageInfo.list as entity>
                    <tr>
                        <td>${entity_index + 1}</td>
                        <td>${entity.name}</td>
                        <td>${(entity.orderNum)!}</td>
                        <td>${(entity.account)!}</td>

                    <#--<td>
                        <#if entity.state==1 >
                            推荐
                        <#elseif entity.state==0>
                            正常
                        <#elseif entity.state==-1>
                            禁用
                        </#if>
                    </td>-->

                        <td>
                            <a href="javascript:" class="btn btn-info btn-xs inputBtn" data-json='${entity.json}'>
                                <span class="glyphicon glyphicon-pencil"></span> 修改名称
                            </a>
                            |
                            <a href="javascript:" class="btn btn-info btn-xs delete_Btn" data-json='${entity.json}'>
                                <span class="glyphicon glyphicon-trash"></span> 删除
                            </a>
                        </td>
                    </tr>
                </#list>
            </table>

            <#include "../common/page.ftl">
        </div>
    </div>
</div>

<#--模态框必须是body的子元素-->
<div class="modal fade" id="inputModal" tabindex="-1" role="dialog" aria-hidden="true">
    <div class="modal-dialog">
        <div class="modal-content">
        <#--模态框标题-->
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>
                <h4 class="modal-title">新增/编辑</h4>
            </div>
        <#--模态框正文-->
            <div class="modal-body">
            <#--填充编辑界面-->
                <form class="form-horizontal" action="/insurance/saveOrUpdate.do" method="post" id="insuranceForm"
                      enctype="multipart/form-data">
                    <input type="hidden" name="id"/>
                    <div class="form-group">
                        <label class="col-lg-4 control-label">名称：</label>
                        <div class="col-lg-6">
                            <input type="text" class="form-control" name="name" placeholder="请输入新名称"/>
                        </div>
                    </div>

                <#-- <div class="form-group">
                     <label class="col-lg-4 control-label">状态：</label>
                     <div class="col-lg-6">
                         <select name="state" class="form-control">
                             <option value="1">推荐</option>
                             <option value="0">正常</option>
                             <option value="-1">禁用</option>
                         </select>
                     </div>
                 </div>-->
                </form>
            </div>
        <#--模态框底部按钮-->
            <div class="modal-footer">
                <button type="button" class="btn btn-default" data-dismiss="modal">关闭</button>
                <button type="button" class="btn btn-primary insuranceSubmitBtn">保存</button>
            </div>
        </div>
    </div>
</div>
<script>
    $(function () {
        /*-------------------------------------------------------------------------------------------------------*/
        $(".inputBtn").click(function () {
            jQuery.validator.messages.required = "";

            // 清空模态框数据
            $("#inputModal input").val("");

            // 获取当前按钮中的自定义JSON对象
            var obj = $(this).data("json");

            //只有编辑按钮才做数据回显
            if (obj) {
                $("#inputModal input[name='id']").val(obj.id);
                $("#inputModal input[name='name']").val(obj.name);
            }
            $("#inputModal").modal('show');
        });
        /*-------------------------------------------------------------------------------------------------------*/

        //声明一个函数，执行表单的验证
        function validform() {
            return $("#insuranceForm").validate({
                rules: {
                    name: {
                        required: true
                    }
                }
            })
        }

        /*-------------------------------------------------------------------------------------------------------*/
        // 点击表单提交按钮触发点击事件，验证表单通过发起GET请求，不通过在表单里提示
        $(".insuranceSubmitBtn").click(function () {
            jQuery.validator.messages.required = "必填项";
            validform().form();
            $("#insuranceForm").ajaxForm(function (data) {
                // 操作成功刷新界面
                if (data.success) {
                    location.reload();
                } else {
                    // 操作失败,提交用户错误信息
                    $.messager.alert("温馨提示", data.msg);
                }
            });
            // 提交表单
            $("#insuranceForm").submit();
        });
        /*-------------------------------------------------------------------------------------------------------*/

    });

</script>

</body>
</html>